package com.gitee.feizns.explore.data_structure.tree.bs.mid;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 面试题32 - I. 从上到下打印二叉树
 * @author feizns
 * @since 2020/3/20
 */
public class LevelOrder {

    public static void main(String[] args) {

    }

    public int[] levelOrder(TreeNode root) {
        List<Integer> ret = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if ( root != null )
            queue.add(root);
        while ( !queue.isEmpty() ) {
            TreeNode curt = queue.remove();
            ret.add(curt.val);
            if ( curt.left != null )
                queue.add(curt.left);
            if ( curt.right != null )
                queue.add(curt.right);
        }
        return ret.stream().mapToInt(Integer::intValue).toArray();
    }

}
